local LayerBase = require("app.Base.LayerBase")

local SetCreatNewMapLayer = class("SetCreatNewMapLayer", LayerBase)

local defaultDatas = {
    map_x = 10,
    map_y = 10,
    angle_x = 0,
    angle_y = 90,
    rand = 2,
    height = 5,
    scale = 1.3,
}

--设置新建新地图参数层
function SetCreatNewMapLayer:ctor(data)
    print("SetCreatNewMapLayer:ctor")
    SetCreatNewMapLayer.super.ctor(self,data)    
    self.datas = {}

    self:initUI()
end

function SetCreatNewMapLayer:onEnter()
    print("SetCreatNewMapLayer:onEnter")
    self:AddButtonClick()
    self:ClickGet()
end

function SetCreatNewMapLayer:initUI() 
    SetCreatNewMapLayer.super.initUI(self) 
    self.bg = display.newSprite("UI/bg/SetCreatNewMapLayer_bg.jpg")
    self.bg:setOpacity(245)
    td.AddRelaPos(self.p_bg,self.bg)

    self.middleNode = display.newNode()
    td.AddRelaPos(self.bg,self.middleNode)
    
    self.text_ditudaxiao = td.CreateLabel("地图大小"):addTo(self.middleNode):move(0,250)

    self.map_x = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.map_x:setFontSize(24)
    self.map_x:setMaxLength(3)
    self.map_x:setPlaceHolder("X")
    self.map_x:addTo(self.middleNode):move(-60,200)
    self.map_x:setInputMode(cc.EDITBOX_INPUT_MODE_SINGLELINE)       ---editBox限制输入为一行


    self.text_X = td.CreateLabel("X"):addTo(self.middleNode):move(0,200)

    self.map_y = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.map_y:setFontSize(24)
    self.map_y:setMaxLength(3)
    self.map_y:setPlaceHolder("Y")
    self.map_y:addTo(self.middleNode):move(60,200)

    self.text_fushijiao = td.CreateLabel("设置X偏斜或Y轴俯视角"):addTo(self.middleNode):move(0,150)

    self.angle_x = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.angle_x:setFontSize(24)
    self.angle_x:setMaxLength(2)
    self.angle_x:setPlaceHolder("X(0~89)")
    self.angle_x:addTo(self.middleNode):move(-60,100)

    self.angle_y = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.angle_y:setFontSize(24)
    self.angle_y:setMaxLength(2)
    self.angle_y:setPlaceHolder("Y(1~90)")
    self.angle_y:addTo(self.middleNode):move(60,100)


    self.text_rand = td.CreateLabel("地边宽度"):addTo(self.middleNode):move(-150,-150)

    self.rand = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.rand:setFontSize(24)
    self.rand:setMaxLength(2)
    self.rand:setPlaceHolder("(防止UI挡住地图)")
    self.rand:addTo(self.middleNode):move(-150,-200)

    self.text_gaodu = td.CreateLabel("缩放大小倍数"):addTo(self.middleNode):move(0,-150)

    self.scale = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.scale:setFontSize(24)
    self.scale:setMaxLength(5)
    self.scale:setPlaceHolder("缩放")
    self.scale:addTo(self.middleNode):move(0,-200)

    self.text_gaodu = td.CreateLabel("初始高度"):addTo(self.middleNode):move(150,-150)

    self.height = ccui.EditBox:create(cc.size(100, 50), "UI/s9/9.png")
    self.height:setFontSize(24)
    self.height:setMaxLength(2)
    self.height:setPlaceHolder("高度")
    self.height:addTo(self.middleNode):move(150,-200)

    self.CreatMap_btn = td.CreateBtn( "创建"):addTo(self.middleNode):move(480,-280)
    self.Get_btn = td.CreateBtn("读取默认设置"):addTo(self.middleNode):move(-250,-280)
    self.Save_btn = td.CreateBtn("保存为默认（未完成）"):addTo(self.middleNode):move(50,-280)
end

function SetCreatNewMapLayer:AddButtonClick()
    td.BtnAddTouch(self.CreatMap_btn, handler(self,self.ClickCreatMap))
    td.BtnAddTouch(self.Get_btn, handler(self,self.ClickGet))
    td.BtnAddTouch(self.Save_btn, handler(self,self.ClickSave))

end

function SetCreatNewMapLayer:ClickCreatMap(btn)
    print("SetCreatNewMapLayer:ClickCreatMap")
    local map_x = tonumber(self.map_x:getText()) 
    local map_y = tonumber(self.map_y:getText())    
    local angle_x = tonumber(self.angle_x:getText()) 
    local angle_y = tonumber(self.angle_y:getText())
    local rand = tonumber(self.rand:getText()) 
    local height = tonumber(self.height:getText())
    local scale = tonumber(self.scale:getText()) 
     
    local datas ={
        map_x = map_x,
        map_y = map_y,
        angle_x = angle_x,
        angle_y = angle_y,
        rand = rand,
        height = height,
        scale = scale,
    }
--    dump(defaultDatas)
    for name, var in pairs(defaultDatas) do
        if datas[name] == nil then 
            datas[name] = defaultDatas[name]
        end
    end   
--    dump(datas)

    datas.angle_x = datas.angle_x>89 and 89 or datas.angle_x
    datas.angle_x = datas.angle_x<0 and 0 or datas.angle_x
    datas.angle_y = datas.angle_x>90 and 90 or datas.angle_y
    datas.angle_y = datas.angle_y<1 and 1 or datas.angle_y  

    

    local scene = require("app.scenes.MapEditorScene").new(datas)
	display.runScene(scene)
end

function SetCreatNewMapLayer:ClickGet(btn)
    print("读取默认设置")
    self.map_x:setText(defaultDatas.map_x)
    self.map_y:setText(defaultDatas.map_y)
    self.angle_x:setText(defaultDatas.angle_x)
    self.angle_y:setText(defaultDatas.angle_y)
    self.rand:setText(defaultDatas.rand)
    self.height:setText(defaultDatas.height)
    self.scale:setText(defaultDatas.scale)
    
end

function SetCreatNewMapLayer:ClickSave(btn)
    print("SetCreatNewMapLayer:ClickSave")

end
return SetCreatNewMapLayer